/** SSUI Mobile Lab 
 *  Created by Nisha Kurani
 *  08/25/2010
 */
package cmu.mobile.lab;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;

public class EditInfo extends Activity {
    /** Called when the activity is first created. */
	Intent result;
	String userDisplayName;
	String userPhone;
	EditText enteredName;
	EditText enteredPhone;
	private static final int GET_CAMERA_CODE = 2;
	private static final int SELECT_IMAGE = 4;
	public static final String GET_PICTURE_CODE = "savedPictureEdit";
	public static final String GET_NAME_CODE = "savedNameEdit";
	public static final String GET_NUMBER_CODE = "savedNumberEdit";
	Bitmap picture;
	ImageView profilePicture;
	Uri uri;
	String picturePath;
	SharedPreferences prefs;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_editinfo);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //Intent i = getIntent();
		result = new Intent();
		enteredName = (EditText)findViewById(R.id.nameBox);
		profilePicture = ((ImageView)findViewById(R.id.profile_pic));
		enteredPhone = (EditText)findViewById(R.id.phoneBox);

		
		//((ImageView)findViewById(R.id.profile_pic)).setImageBitmap(picture);
		
		prefs = PreferenceManager.getDefaultSharedPreferences(EditInfo.this);
		picturePath = prefs.getString(GET_PICTURE_CODE, picturePath);
		userDisplayName = prefs.getString(GET_NAME_CODE, userDisplayName);
		userPhone = prefs.getString(GET_NUMBER_CODE, userPhone);
		
		if (picturePath != null)
		{
			Uri u = Uri.parse("content://media" + picturePath);
			profilePicture.setImageURI(u);
		}
		if (userDisplayName != null)
		{
			enteredName.setText(userDisplayName);
		}
		if (userPhone != null)
		{
			enteredPhone.setText(userPhone);
		}
		
		// back button
		findViewById(R.id.BackButton).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setResult(Activity.RESULT_CANCELED);
				finish();
			}
		});
		
		//delete text when name box selected
		findViewById(R.id.nameBox).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if ( userDisplayName == null || userDisplayName.equals(R.string.commentText))
					enteredName.setText("");
			}
		});
		
		//delete text when phone box selected
		findViewById(R.id.phoneBox).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if ( userPhone == null || userPhone.equals(R.string.commentText))
					enteredPhone.setText("");
			}
		});
		
		// save button
		findViewById(R.id.SaveButton).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//set null string back to "Set Name"
				userDisplayName = enteredName.getText().toString();
				if(userDisplayName == null)
				{
					userDisplayName = "Set Name";
				}
				
				
				userPhone = enteredPhone.getText().toString();
				//check if number was inputed correctly
				//save phone number to database
				
				prefs = PreferenceManager.getDefaultSharedPreferences(EditInfo.this);
        		SharedPreferences.Editor spe = prefs.edit();
        		spe.putString(GET_PICTURE_CODE, picturePath);
        		spe.putString(GET_NAME_CODE, userDisplayName);
        		spe.putString(GET_NUMBER_CODE, userPhone);
        		spe.commit();
				
				
				result.putExtra("username", userDisplayName);
				result.putExtra("image", picturePath);
    			setResult(Activity.RESULT_OK, result);
    			finish();
			}
		});
		
		findViewById(R.id.CameraButton).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v){
				Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
				startActivityForResult(cameraIntent, GET_CAMERA_CODE);
			}
		});
		
		findViewById(R.id.UploadButton).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v){
				Intent uploadIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
				startActivityForResult(uploadIntent, SELECT_IMAGE);
			}
		});
	}
	
	// OnActivityResult event handler (needed to handle the results returned by
	// sub-Activities
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
			case (GET_CAMERA_CODE): {
				if (resultCode == Activity.RESULT_OK) {
					picture = (Bitmap) data.getExtras().get("data");
					((ImageView)findViewById(R.id.profile_pic)).setImageBitmap(picture);
	                ContentValues values = new ContentValues();
	                	values.put(Images.Media.TITLE, "title");
	                	values.put(Images.Media.BUCKET_ID, "test");
	                	values.put(Images.Media.DESCRIPTION, "test Image taken");
	                	values.put(Images.Media.MIME_TYPE, "image/jpeg");
	                uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
	                
	                picturePath = uri.getPath();
	                prefs = PreferenceManager.getDefaultSharedPreferences(EditInfo.this);
	        		SharedPreferences.Editor spe = prefs.edit();
	        		spe.putString(GET_PICTURE_CODE, picturePath);
	        		spe.commit();
	                
	        		OutputStream outputstream;
	                try {
	                        outputstream = getContentResolver().openOutputStream(uri);

	                        picture.compress(Bitmap.CompressFormat.JPEG, 70, outputstream);
	                        outputstream.close();
	                } catch (FileNotFoundException e) {
	                        //
	                }catch (IOException e){
	                        //
	                }
				}
				break;
			}
			case(SELECT_IMAGE):{
				if (requestCode == SELECT_IMAGE)
				    if (resultCode == Activity.RESULT_OK) {
				      uri = data.getData();
				      picturePath = uri.getPath();
				      	Uri u = Uri.parse("content://media" + picturePath);
						profilePicture.setImageURI(u);
						
						prefs = PreferenceManager.getDefaultSharedPreferences(EditInfo.this);
		        		SharedPreferences.Editor spe = prefs.edit();
		        		spe.putString(GET_PICTURE_CODE, picturePath);
		        		spe.commit();
				      
				    } 
				}

			}
			
		}
	}


